package com.ct.gd.passport.view;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.ct.gd.passport.R;
import com.up91.common.android.async.ILoading;
import com.up91.common.android.async.SimpleAsyncTask;

public abstract class RootCatalogFragment<T> extends Fragment implements OnCheckedChangeListener {
	private static final long					DURATION						= 500;

	private View								mIndicator;
	
	private Set<OnRootChangedListener<T>>	mOnRootChangedListeners	= new LinkedHashSet<RootCatalogFragment.OnRootChangedListener<T>>();

	public interface OnRootChangedListener<T> {
		void onRootChanged(T root);
	}

	private void notifyOnRootChanged(T bankType) {
		for (OnRootChangedListener<T> listener : mOnRootChangedListeners) {
			listener.onRootChanged(bankType);
		}
	}

	public boolean registerOnRootChangedListener(OnRootChangedListener<T> listener) {
		return mOnRootChangedListeners.add(listener);
	}

	public boolean unregisterOnRootChangedListener(OnRootChangedListener<T> listener) {
		return mOnRootChangedListeners.remove(listener);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View v = inflater.inflate(R.layout.catalog_scope, null);
		return v;
	}

	public void loadBankType() {
		new QueryRootCatalogTask((ILoading) getActivity()).execute();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loadBankType();
	}

	protected abstract List<T> load();
	
	protected abstract String getDisp(T root);
	
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		View checkedView = group.findViewById(checkedId);
		T root = (T) checkedView.getTag();
		notifyOnRootChanged(root);
		
		indicate(mIndicator.getLeft(), checkedView.getLeft());
	}

	private class QueryRootCatalogTask extends SimpleAsyncTask<Void, Void, List<T>> {

		public QueryRootCatalogTask(ILoading context) {
			super(context);
		}

		@Override
		protected List<T> onLoad(Void... params) {
			return load();
		}

		@Override
		protected void onUI(List<T> result) {
			LayoutInflater inflater = LayoutInflater.from(getActivity());
			final RadioGroup tabs = (RadioGroup) getView().findViewById(R.id.area_catalog_scopes);
			tabs.setOnCheckedChangeListener(RootCatalogFragment.this);
			for (T root : result) {
				inflater.inflate(R.layout.catalog_scope_item, tabs);
				RadioButton tab = (RadioButton) tabs.getChildAt(tabs.getChildCount() - 1);
				tab.setText(getDisp(root));
				tab.setId(tabs.getChildCount());
				tab.setTag(root);
			}

			mIndicator = getView().findViewById(R.id.tab_indicator);
			// 初始化indicator长度
			int indicatorWidth = tabs.getWidth() / tabs.getChildCount();
			//TODO fuck!!! It doesn't work, which works very well in EduMobi.
			mIndicator.getLayoutParams().width = indicatorWidth;
			// work around
//			mIndicator.setMinimumWidth(indicatorWidth);
//			mIndicator.invalidate();
			// 默认选中第一项
			tabs.check(1);
			mIndicator.post(new Runnable() {
				@Override
				public void run() {
					// 初始化indicator长度
					int indicatorWidth = tabs.getWidth() / tabs.getChildCount();
					//TODO fuck!!! It doesn't work, which works very well in EduMobi.
					mIndicator.getLayoutParams().width = indicatorWidth;
					// work around
//					mIndicator.setMinimumWidth(indicatorWidth);
//					mIndicator.invalidate();
					// 默认选中第一项
//					tabs.check(1);
				}
			});
		}
	}

	private void indicate(int srcLeft, int destLeft) {
		TranslateAnimation anim = new TranslateAnimation(srcLeft - destLeft, 0f, 0f, 0f);
		anim.setDuration(DURATION);
		anim.setFillAfter(true);
		anim.setInterpolator(new BounceInterpolator());
		mIndicator.layout(destLeft, mIndicator.getTop(), destLeft + mIndicator.getWidth(), mIndicator.getBottom());
		mIndicator.startAnimation(anim);
	}
}
